<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: HCZ <huangchengze@etrol.com> 
// +----------------------------------------------------------------------

namespace app\index\controller\ajax;
use think\config\driver\Json;
//公司关联下拉菜单值

class Map {
	/**
	 * 航拍图图层
	 * @return \think\response\Json
	 */
	public function aerialPhotoLayers(){
		$aerialPhotoList = db('aerialphoto')->select();
		return json($aerialPhotoList);
	}
	
	/**
	 * 企业信息
	 */
	public function enterpriseMarkers($scope = null){
		$where = [];
		if($scope){
			$where['a.id'] = $scope;
		}
		$enterpriseMarkers = db('enterprise')
								->alias('a')
								->join('earlywarning b','a.id=b.enterpriseId')
								->where($where)
								->field('a.id,shortname,latitudeLongitude,riskDegree')
								->select();
		return json($enterpriseMarkers);
	}
	
	/**
	 * 企业监控源
	 * @param string $enterprise 企业ID
	 * @return Json
	 */
	public function dangerSources($enterprise = null){
		$dangerSource = collection(model('basic/dangersource')->with('sensors')->where('enterpriseId',$enterprise)->select())->toArray();
		return json($dangerSource);
	}
	
	/**
	 * 企业摄像头
	 * @param string $enterprise
	 * @return Json
	 */
	public function cameras($enterprise = null){
		$cameras = db('securitycamera')->where('enterpriseId',$enterprise)->select();
		return json($cameras);
	}
	  
	  /**
	   * 审核事项
	   * @return Json
	   */
	  public function approveList(){
	  	$model = model('Wfitem');
	  	$where = [] ;
	  	if(input('where') !== null && input('where') !== '*'){
	  		$where['status'] = input('where');
	  	}
	  	if(input('scope')){
	  		$where['enterpriseId'] = input('scope');
	  	}
	  	if(($limit = input('limit')) && ($page = input('page'))){
	  		$startLimit =  ($page-1)*$limit;
	  	
	  		$info = $model->where($where)->limit($startLimit.','.$limit)->order('id desc')->select();
	  	}else{
	  		$info = $model->order('id desc')->select();
	  	}
	  	$count = $model->where($where)->count('id');
	  	foreach ($info as &$item){
	  		if(in_array($item->wfTypeId,[3,6,10,12])){
	  			$business = model($item->busiModel)->with('enterprise')->where('id',$item['busiId'])->find();
	  		}
	  		if(in_array($item->wfTypeId,[7,8,9,13])){
	  			$business = model($item->busiModel)->where('id',$item['busiId'])->find();
	  		}
	  		if(in_array($item->wfTypeId,[1,4])){
	  			$business = model($item->busiModel)->with('unit')->where('id',$item['busiId'])->find();
	  		}
	  		if($item->wfTypeId == 5){
	  			$business = model($item->busiModel)->with('enterprise')->where('id',$item['busiId'])->find();
	  		}
	  		if ($item->wfTypeId == 11){
	  			$business = model($item->busiModel)->where('id',$item['busiId'])->find();
	  		}
	  		$item['business'] = $business;
	  	}
	    $info = collection($info)->toArray();
	  	//管理员用户列表 
	  	return json(['code'=>1,'msg'=>'用户信息','count'=>$count,'data'=>$info,'scope'=>input('scope')]);
	  }
	  /**
	   * 忽略审核事项
	   * @param string $id
	   */
	  public function ignore($id = null){
	  	if($id){
	  		if(model('Wfitem')->where('id',$id)->update(['status'=>4])){
	  			return json(['code'=>1,'msg'=>'已忽略']);
	  		}
	  	}
	  	return json(['code'=>'0','msg'=>'失败']);
	  }
	  
	  
	  /**
	   * 开始审核事项
	   * @param string $id
	   */
	  public function approve($id = null){
	  	if($id){
	  		$wfItem = model('Wfitem')->find($id);
			if($wfItem->isUpdate(true)->save(['status'=>1,'wfStep'=>1]) !== false){
		  		$workflow = model('Workflow')->with('steps')->find($wfItem['wfFlowInfoId']);
		  		$data['wfItemInfoId'] = $id;
		  		$data['operationId'] = $workflow->steps[0]->chargePerson;
		  		$data['wfStepInfoId'] = $workflow->steps[0]->id;
		  		if(model('Wfprocess')->save($data) !== false){
		  			return  json(['code'=>1,'msg'=>'已申请']);
		  		}
			}
	  	}
	  	return json(['code'=>'0','msg'=>'失败']);
	  }
	  
	  /**
	   * 快速搜索菜单
	   * @param string $keyword 关键字
	   * @return json
	   */
	  public function searchMenu($keyword = null){
	  	$where = array('level' =>['<>',2]);
	  	if($keyword){
	  		$menu = model('Rule')
	  				->where($where)
	  				->where('url','like','%'.$keyword.'%')
	  				->whereOr('title','like','%'.$keyword.'%')
	  				->field('url,title')
	  				->select();
	  		return json(['code'=>1,'msg'=>'','data'=>$menu]);
	  	}else{
	  		return json(['code'=>0,'msg'=>'','data'=>null]);
	  	}
			  	
	  }
}